﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;

namespace WpfApplication1
{
    public class ImageGifView : MediaElement
    {
        public ImageGifView()
        {
            this.Loaded += ImageGifView_Loaded;
        }

        public string imgSource { get; set; }

        public bool IsOncePlay { get; set; }

        private void ImageGifView_Loaded(object sender, RoutedEventArgs e)
        {
            //循环播放
            if (!IsOncePlay)
            {
                this.MediaEnded -= ImageGifView_MediaEnded;
                this.MediaEnded += ImageGifView_MediaEnded;
            }
            this.Source = new Uri( imgSource, UriKind.Absolute);
            this.LoadedBehavior = MediaState.Manual;
            this.Stretch = System.Windows.Media.Stretch.Fill;
            this.Play();

        }

        /// <summary>
        /// 结束后事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ImageGifView_MediaEnded(object sender, RoutedEventArgs e)
        {
            MediaElement media = (MediaElement)sender;
            media.Position = TimeSpan.FromMilliseconds(1);
            media.Play();
        }

        internal void Load()
        {
            RoutedEventArgs args = new RoutedEventArgs(MediaElement.LoadedEvent);
            this.RaiseEvent(args);
        }
    }
}
